package com.utoronto.syncgallery;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.utoronto.syncgallery.IF.GalleryFileExplorer;
import com.utoronto.syncgallery.adapter.IconAdapter;

/**
 * SearchActivity
 * 
 * @author KOBE, Daniel
 * 
 */

public class SearchActivity extends Activity implements GalleryFileExplorer,
		OnItemClickListener {

	private static File curDir;
	private static ArrayList<File> results;
	private GridView gallery;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gallery);

		/** Construct an arraylist to save search result. */
		if (results == null)
			results = new ArrayList<File>();

		/** Get the intent, verify the action and get the query. */
		Intent intent = getIntent();
		if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
			/** Perform search.*/
			doSearch(intent);
			
			/** Show search result using grid view. */
			gallery = (GridView) findViewById(R.id.gallery_view);
			setTitle(curDir.getName().compareTo("") == 0 ? curDir.getPath()
					: curDir.getName());

			gallery.setOnItemClickListener(this);
			gallery.setAdapter(new IconAdapter(this));
		}
	}

	@Override
	public void onNewIntent(final Intent query) {
		super.onNewIntent(query);
		/** Get the intent, verify the action and get the query. */
		if (Intent.ACTION_SEARCH.equals(query.getAction())) {
			/** Perform Search. */
			doSearch(query);
			
			/** Show search result using grid view. */
			gallery = (GridView) findViewById(R.id.gallery_view);
			setTitle(curDir.getName().compareTo("") == 0 ? curDir.getPath()
					: curDir.getName());
			gallery.setAdapter(new IconAdapter(this));
		}
	}

	private void doSearch(final Intent query) {
		/** Clear results if the user searches before. */
		results.clear();

		/** Get the input keyword from search bar. */
		final String keyword = query.getStringExtra(SearchManager.QUERY);
		/** Get image file extension. */
		final String[] imageExt = getResources().getStringArray(
				R.array.fileEndingImage);
		/** Only search images in current directory. */
		curDir = new File(query.getBundleExtra(SearchManager.APP_DATA)
				.getString("curDir"));

		/** Search images with file name containing the keyword. */
		for (File file : curDir.listFiles(new FileFilter() {
			public boolean accept(File filePath) {
				return !filePath.isDirectory();
			}
		})) {
			for (String ext : imageExt) {
				if (file.getName().toLowerCase().endsWith(ext)
						&& file.getName().contains(keyword)) {
					results.add(file);
				}
			}
		}
	}

	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		final File file = results.get((int) id);
		/** Open the picture in full screen */
		Intent fullScreen = new Intent(this, ViewpicActivity.class);
		fullScreen.putExtra("file", file);
		startActivity(fullScreen);
	}

	/** Show current directory. */
	public File getmCurrentDir() {
		return curDir;
	}

	/** Show search results. */
	public ArrayList<File> getmFiles() {
		return results;
	}

	/** Show images extension. */
	public String[] getmImageExt() {
		return getResources().getStringArray(R.array.fileEndingImage);
	}

}
